package com.sdp.irrigation.kafka.biz.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sdp.irrigation.cache.EmpCache;
import com.sdp.irrigation.db.irrigation_emp.service.IIrrigationEmpService;
import com.sdp.irrigation.entity.IrrigationEmp;
import com.sdp.irrigation.kafka.biz.IProtocolBiz;
import com.sdp.irrigation.kafka.producer.KafkaProducer;
import com.sdp.irrigation.kafka.property.ServerProperty;
import com.sdp.irrigation.kafka.utils.ProtocolUtils;
import com.sdp.irrigation.protocol.CommPackage;
import com.sdp.irrigation.utils.CommConstants;
import com.sdp.irrigation.utils.HttpClientUtil;
import com.sdp.irrigation.utils.JsonUtils;
import com.sdp.irrigation.utils.ServerResponseEnum;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class QueryBiz implements IProtocolBiz {

	@Autowired
	private KafkaProducer kafkaProducer;
	
	@Autowired
	private HttpClientUtil httpClientUtil;
	
	@Autowired
	private ServerProperty serverProperty;
	
	@Autowired 
	private IIrrigationEmpService empService;
	
	@Autowired 
	private EmpCache empCache;
	
	@Override
	public void execute(List<CommPackage> packageList) {
		if (packageList.size() > 0) {
			for (CommPackage commPackage : packageList) {
				String devSn = CommConstants.getQueryBalanceCommPackageDevSn(commPackage);
				String cardNum = commPackage.getCardNum();
				if (!StringUtils.isEmpty(cardNum)) {
					Integer balance = getIcBalance(cardNum);
					IrrigationEmp emp = empCache.getEmp(cardNum);
					CommPackage responsePackage = ProtocolUtils.getDownBalanceResultCommPackage(devSn, cardNum, balance, emp);
					kafkaProducer.send(ProtocolUtils.getDownMessageTopic(String.valueOf(ServerResponseEnum.BALANCE_RESULT.getCode()), serverProperty.getDownTopicPrefix()), JsonUtils.toJSONString(responsePackage));
				}
			}
		}
		
	}
	
	public static void main(String[] args) {
		String s = "{\"msg\":\"账户信息不存在\",\"code\":1004002001}";
		JSONObject result = JSON.parseObject(s);
		System.out.println(result.containsKey("data"));
	}
	
	public Integer getIcBalance(String cardNum) {
		int resultMoney = 0;
		if (CommConstants.isProvinceIcNo(cardNum)) {
			JSONObject result = httpClientUtil.getUserICInfo(cardNum, null);
			log.debug("ic user info: {}", JsonUtils.toJSONString(result));
			if (result.getJSONObject("data") != null && result.getJSONObject("data").containsKey("balance")) {
				resultMoney = result.getJSONObject("data").getInteger("balance");
			}
			//添加更新用户余额的操作
			UpdateWrapper<IrrigationEmp> updateWrapper = new UpdateWrapper<>();
			updateWrapper.lambda().eq(IrrigationEmp::getEmpIc, cardNum)
				.set(IrrigationEmp::getIcMoney, resultMoney);
			empService.update(updateWrapper);
			
			empCache.deleteCache(cardNum);
		} else {//查询人员信息表
			IrrigationEmp emp = empCache.getEmp(cardNum);
//			QueryWrapper<IrrigationEmp> queryWrapper = new QueryWrapper<>();
//			queryWrapper.lambda().eq(IrrigationEmp::getEmpIc, cardNum);
//			IrrigationEmp emp = empService.getOne(queryWrapper, false);
			if (emp != null && emp.getIcMoney() != null) {
				resultMoney = emp.getIcMoney().intValue();
			}
		}
		return resultMoney;
	}

}
